package com.qztech.sdk.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public abstract class BaseFragment extends Fragment {


    public static final int CMD_SELECTED = 0;
    public static final int CMD_ADDED = 1;
    public static final int CMD_DELETED = 2;
    protected View mRootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mRootView = inflater.inflate(getResourceId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public abstract void onRefreshFragment(int cmd);


    public final <T extends View> T findViewById(@IdRes int id) {
        return mRootView.findViewById(id);
    }
    /**
     * @return
     * @desc 获取View 资源
     */
    protected abstract int getResourceId();

    protected void enableViews(boolean enabled, View... buttonView) {
        for (View button : buttonView) {
            button.setEnabled(enabled);
        }
    }
    protected void enableViewsreverse(boolean enabled,View reverse, View... buttonView) {
        for (View button : buttonView) {
            button.setEnabled(enabled);
        }
        reverse.setEnabled(!enabled);
    }

    protected void checkButton(boolean checked, CompoundButton... buttonView) {
        for (CompoundButton button : buttonView) {
            button.setChecked(checked);
        }
    }
    protected void checkButtonReverse(boolean checked,CompoundButton reverse, CompoundButton... buttonView) {
        for (CompoundButton button : buttonView) {
            button.setChecked(checked);
        }
        reverse.setChecked(!checked);
    }


    protected boolean hasChecked(RadioButton[] radioButtons) {
        for (RadioButton radioButton : radioButtons) {
            if (radioButton.isChecked()) return true;
        }
        return false;
    }

    public void updateByConnected(boolean isConnected){}

}
